/*
 * Project: chapter5
 * File Created: Saturday, 13th November 2021 10:28:32 am
 * Author: hotsuitor (hotsuitor@qq.com)
 * -----
 * Last Modified: Sunday, 14th November 2021 11:53:05 am
 * Modified By: hotsuitor (hotsuitor@qq.com)
 */

package main

import (
	"fmt"
	"log"
	"net"

	pb "chapter5/protobuf"

	"golang.org/x/net/context"
	"google.golang.org/grpc"
)

// 定义服务结构体
type ProgrammerServiceServer struct{}

// 定义被调用的服务方法
func (p *ProgrammerServiceServer) GetProgrammerInfo(ctx context.Context, req *pb.Request) (resp *pb.Response, err error) {
	name := req.Name
	if name == "lili" {
		resp = &pb.Response{
			Uid:      6,
			Username: name,
			Job:      "CTO",
			GoodAt:   []string{"Go", "JavaScript", "Python", "English"},
		}
	}
	err = nil
	return
}

func main() {
	port := ":8078"
	l, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatal("listen error: %v\n", err)
	}
	fmt.Printf("listen %s\n", port)
	s := grpc.NewServer()
	// 将 ProgrammerService 注册到 gRpc 中
	// 注意第2个参数 ProgrammerServiceServer 是接口类型的变量，需要取地址传参
	pb.RegisterProgrammerServiceServer(s, &ProgrammerServiceServer{})
	s.Serve(l)
}
